﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GetServiceReference;

public partial class SearchPage : System.Web.UI.Page
{
    String itemName;
    Guid categoryID;
    float minValue;
    float maxValue;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {

        }
        // get input for search
        itemName = Request.QueryString["name"];
        String category = Request.QueryString["category"];
        String price = Request.QueryString["price"];

        if (itemName == null)
        {
            itemName = "All";
        }

        if (category == null)
        {
            categoryID = default(Guid);
        }
        else
        {
            try
            {
                categoryID = new Guid(category);
            }
            catch
            {
                categoryID = default(Guid);
            }
        }
        minValue = 0;
        maxValue = float.MaxValue;
        if (price != null)
        {
            if (price != "All" && !price.Equals("1000+"))
            {
                int i = price.IndexOf('-');
                minValue = Int32.Parse(price.Substring(0, i));
                maxValue = Int32.Parse(price.Substring(i + 1));
            }
            else
            {
                if (price.Equals("1000+"))
                {
                    minValue = 1000;
                }
            }
        }
        // search item
        GetItemServiceClient gic = new GetItemServiceClient();
        Item[] list = gic.getAll(itemName, categoryID, default(Guid), minValue, maxValue, 5000);
        if (list == null || list.Length == 0)
        {
            noitemAlear.Text = "No item found";
        }
        // paging page
        PagePaging.DataSource = list.ToList();
        PagePaging.BindToControl = listItem;
        listItem.DataSource = PagePaging.DataSourcePaged;
    }


    protected void sortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        // search item
        GetItemServiceClient gic = new GetItemServiceClient();
        List<Item> list = gic.getAll(itemName, categoryID, default(Guid), minValue, maxValue, 5000).ToList();
        // sort by
        if (sortBy.SelectedValue.Equals("New"))
        {
            list = list.OrderByDescending(q => q.Bid_StartDate).ToList();
        }
        if (sortBy.SelectedValue.Equals("Decrement price"))
        {
            list = list.OrderByDescending(q => q.CurrentPrice).ToList();
        }
        if (sortBy.SelectedValue.Equals("Increment price"))
        {
            list = list.OrderBy(q => q.CurrentPrice).ToList();
        }
        // paging page
        PagePaging.DataSource = list.ToList();
        PagePaging.BindToControl = listItem;
        listItem.DataSource = PagePaging.DataSourcePaged;
    }
}